package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private ApartmentInfoService apartmentInfoService;
    @Autowired
    private AttrValueMapper attrValueMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;
    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private PaymentTypeMapper paymentTypeMapper;
    @Autowired
    private LeaseTermMapper leaseTermMapper;
    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {
        //先判断是跟新操作还是新增
        Long roomId = roomSubmitVo.getId();
        //更新操作
        if(roomId != null){
            //删除房间配套信息
            QueryWrapper<RoomFacility> facilityQueryWrapper = new QueryWrapper<>();
            facilityQueryWrapper.eq("room_id", roomId);
            roomFacilityService.remove(facilityQueryWrapper);

            //删除房间属性信息
            QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
            roomAttrValueQueryWrapper.eq("room_id", roomId);
            roomAttrValueService.remove(roomAttrValueQueryWrapper);

            //删除房间标签信息
            QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
            roomLabelQueryWrapper.eq("room_id", roomId);
            roomLabelService.remove(roomLabelQueryWrapper);

            //删除房间支付方式
            QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
            roomPaymentTypeQueryWrapper.eq("room_id", roomId);
            roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

            //删除房间可选租期
            QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
            roomLeaseTermQueryWrapper.eq("room_id", roomId);
            roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

            //删除房间图片
            QueryWrapper<GraphInfo> graphQueryWrapper = new QueryWrapper<>();
            graphQueryWrapper.eq("item_type", ItemType.ROOM).eq("item_id", roomId);
            graphInfoService.remove(graphQueryWrapper);

        }

        //插入房间信息
        super.saveOrUpdate(roomSubmitVo);
        roomId = roomSubmitVo.getId();

        //插入房间配套信息
        List<Long> facilityInfoIds = roomSubmitVo.getFacilityInfoIds();
//        List<RoomFacility> roomFacilities = new ArrayList<>();
//        for (Long facilityInfoId : facilityInfoIds) {
//            RoomFacility room = RoomFacility.builder().build();
//            room.setRoomId(roomId);
//            room.setFacilityId(facilityInfoId);
//            roomFacilities.add(room);
//        }
        List<RoomFacility> roomFacilities = facilityInfoIds.stream().map(facilityInfoId -> {
            RoomFacility room = RoomFacility.builder().build();
            room.setRoomId(roomSubmitVo.getId());
            room.setFacilityId(facilityInfoId);
            return room;
        }).collect(Collectors.toList());

        roomFacilityService.saveBatch(roomFacilities);

        //插入房间属性信息
        List<Long> attrValueIds = roomSubmitVo.getAttrValueIds();
//        List<RoomAttrValue> roomAttrValues = new ArrayList<>();
//        for (Long attrValueId : attrValueIds) {
//            RoomAttrValue room = RoomAttrValue.builder().build();
//            room.setRoomId(roomId);
//            room.setAttrValueId(attrValueId);
//            roomAttrValues.add(room);
//        }
        List<RoomAttrValue> roomAttrValues = attrValueIds.stream().map(attrValueId -> {
            RoomAttrValue room = RoomAttrValue.builder().build();
            room.setRoomId(roomSubmitVo.getId());
            room.setAttrValueId(attrValueId);
            return room;
        }).collect(Collectors.toList());

        roomAttrValueService.saveBatch(roomAttrValues);

        //插入房间标签信息
        List<Long> labelInfoIds = roomSubmitVo.getLabelInfoIds();
        List<RoomLabel> roomLabels = new ArrayList<>();
        for (Long labelInfoId : labelInfoIds) {
            RoomLabel room = RoomLabel.builder().build();
            room.setRoomId(roomId);
            room.setLabelId(labelInfoId);
            roomLabels.add(room);
        }
        roomLabelService.saveBatch(roomLabels);
        //插入房间支付方式信息
        List<Long> paymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        List<RoomPaymentType> roomPaymentTypes = new ArrayList<>();
        for (Long paymentTypeId : paymentTypeIds) {
            RoomPaymentType room = RoomPaymentType.builder().build();
            room.setRoomId(roomId);
            room.setPaymentTypeId(paymentTypeId);
            roomPaymentTypes.add(room);
        }
        roomPaymentTypeService.saveBatch(roomPaymentTypes);

        //插入房间可选租期信息
        List<Long> leaseTermIds = roomSubmitVo.getLeaseTermIds();
        List<RoomLeaseTerm> roomLeaseTerms = new ArrayList<>();
        for (Long leaseTermId : leaseTermIds) {
            RoomLeaseTerm room = RoomLeaseTerm.builder().build();
            room.setRoomId(roomId);
            room.setLeaseTermId(leaseTermId);
            roomLeaseTerms.add(room);
        }
        roomLeaseTermService.saveBatch(roomLeaseTerms);

        //插入房间图片信息
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        List<GraphInfo> graphInfos = new ArrayList<>();
        for (GraphVo graphVo : graphVoList) {
            GraphInfo graphInfo = new GraphInfo();
            graphInfo.setName(graphVo.getName());
            graphInfo.setUrl(graphVo.getUrl());
            graphInfo.setItemType(ItemType.ROOM);
            graphInfo.setItemId(roomId);
            graphInfos.add(graphInfo);
        }
        graphInfoService.saveBatch(graphInfos);
    }

    @Override
    public IPage<RoomItemVo> pageRoomItemByQuery(IPage<RoomItemVo> page, RoomQueryVo queryVo) {
        return roomInfoMapper.pageRoomItemByQuery(page, queryVo);
    }

    @Override
    public RoomDetailVo getRoomDetailById(Long id) {
        //id是房间的id
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        //获取房间信息
        RoomInfo roomInfo = baseMapper.selectById(id);
        BeanUtils.copyProperties(roomInfo, roomDetailVo);

        //获取公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoService.getById(roomInfo.getApartmentId());
        roomDetailVo.setApartmentInfo(apartmentInfo);

        //获取图片列表
        QueryWrapper<GraphInfo> graphQueryWrapper = new QueryWrapper<>();
        graphQueryWrapper.eq("item_type", ItemType.ROOM).eq("item_id", id);
        List<GraphInfo> list = graphInfoService.list(graphQueryWrapper);
        List<GraphVo> graphVoList = new ArrayList<>();
        for (GraphInfo graphInfo : list) {
            GraphVo graphVo = GraphVo.builder().build();
            graphVo.setName(graphInfo.getName());
            graphVo.setUrl(graphInfo.getUrl());
            graphVoList.add(graphVo);
        }
        roomDetailVo.setGraphVoList(graphVoList);

        //获得属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.selectListByRoomId(id);
        roomDetailVo.setAttrValueVoList(attrValueVoList);

        //获取配套信息列表
        List<FacilityInfo>  facilityInfoList = facilityInfoMapper.selectListByRoomId(id);
        roomDetailVo.setFacilityInfoList(facilityInfoList);

        //获取标签信息列表
        List<LabelInfo> labelInfoList =  labelInfoMapper.selectListByRoomId(id);
        roomDetailVo.setLabelInfoList(labelInfoList);

        //支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.selectListByRoomId(id);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        //可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.selectListByRoomId(id);
        roomDetailVo.setLeaseTermList(leaseTermList);
        return roomDetailVo;
    }

    @Override
    public void removeRoomById(Long roomId) {
        //删除房间配套信息
        QueryWrapper<RoomFacility> facilityQueryWrapper = new QueryWrapper<>();
        facilityQueryWrapper.eq("room_id", roomId);
        roomFacilityService.remove(facilityQueryWrapper);

        //删除房间属性信息
        QueryWrapper<RoomAttrValue> roomAttrValueQueryWrapper = new QueryWrapper<>();
        roomAttrValueQueryWrapper.eq("room_id", roomId);
        roomAttrValueService.remove(roomAttrValueQueryWrapper);

        //删除房间标签信息
        QueryWrapper<RoomLabel> roomLabelQueryWrapper = new QueryWrapper<>();
        roomLabelQueryWrapper.eq("room_id", roomId);
        roomLabelService.remove(roomLabelQueryWrapper);

        //删除房间支付方式
        QueryWrapper<RoomPaymentType> roomPaymentTypeQueryWrapper = new QueryWrapper<>();
        roomPaymentTypeQueryWrapper.eq("room_id", roomId);
        roomPaymentTypeService.remove(roomPaymentTypeQueryWrapper);

        //删除房间可选租期
        QueryWrapper<RoomLeaseTerm> roomLeaseTermQueryWrapper = new QueryWrapper<>();
        roomLeaseTermQueryWrapper.eq("room_id", roomId);
        roomLeaseTermService.remove(roomLeaseTermQueryWrapper);

        //删除房间图片
        QueryWrapper<GraphInfo> graphQueryWrapper = new QueryWrapper<>();
        graphQueryWrapper.eq("item_type", ItemType.ROOM).eq("item_id", roomId);
        graphInfoService.remove(graphQueryWrapper);

        //删除房间本身信息
        baseMapper.deleteById(roomId);
    }
}




